<template>
	<div class="main">
		<van-address-edit
			:area-list="areaList"
			show-delete
			show-set-default
			:area-columns-placeholder="['请选择', '请选择', '请选择']"
			@save="onSave"
			@delete="onDelete"
			@change-detail="onChangeDetail"
			:address-info="defaultList"
		/>
	</div>
</template>

<script setup>
import { reactive, toRefs, onMounted, computed, getCurrentInstance, watch, defineEmits, ref } from 'vue';
import { useStore } from 'vuex';
import { areaList } from '@vant/area-data';
import { AddressList, AddressAddIn, AddressDetele } from '@s/address';
const { proxy } = getCurrentInstance();
const data = reactive({
	searchResult: [],
	defaultList: {},
});
if (proxy.$route.query.id) {
	AddressList({ id: proxy.$route.query.id }).then((res) => {
		const flagList = res.data.data[0];
		console.log(flagList.address.substring(flagList.address.indexOf('省') + 1, flagList.address.indexOf('市') + 1));
		data.defaultList = {
			id: flagList.id,
			name: flagList.uname,
			tel: flagList.aphone,
			isDefault: flagList.isdefault == 1 ? true : false,
			// province: flagList.address.substring(0, flagList.address.indexOf('省') + 1),
			// city: flagList.address.substring(flagList.address.indexOf('省') + 2, flagList.address.indexOf('市') + 1),
			// county: flagList.address.substring(flagList.address.indexOf('市') + 2, flagList.address.indexOf('区') + 1),
			addressDetail: flagList.address.substring(flagList.address.indexOf('区') + 2, flagList.address.lastIndexOf('-')),
			areaCode:flagList.address.substring(flagList.address.lastIndexOf('-')+1,flagList.address.length)
		};
	});
}
const { searchResult, defaultList } = toRefs(data);
const onSave = (value) => {
	let params = {
		address:
			value.province == value.city
				? `${value.city}-${value.county}-${value.addressDetail}-${value.areaCode}`
				: `${value.province}-${value.city}-${value.county}-${value.addressDetail}-${value.areaCode}`,
		aphone: value.tel,
		isdefault: value.isDefault ? 1 : 0,
		name: value.name,
		uid: proxy.$store.state.user.userInfo.id,
		id:value.id ? value.id:null
	};
	AddressAddIn(params).then((res) => {
		if (res.code == 200) {
			proxy.$router.push('/addressList');
		}
	});
};
//    const areaCodeInit= async ()=> {
//     // 这里是点击编辑拿到的地址，我是用‘-’拼接起来的，现在分割一下
//       var areaN = this.form.areaName.split("-");
//       var city = areaN[1]; //城市
//       var district = areaN[2]; //区/县
//       _.forEach(this.areaList.city_list, (o, c) => {
//         if (o == city) {
//           let cityId = String(_.take(c, 2));
//           _.forEach(this.areaList.county_list, (i, a) => {
//             if (i == district) {
//               let districtId = String(_.take(a, 2));
//               if (cityId == districtId) {
//                 this.areaCode = a;
//               }
//             }
//           });
//         }
//       });
//     },
const onDelete = () => {
	AddressDetele({ id: proxy.$route.query.id }).then((res) => {
		if (res.code == 200) {
			proxy.$toast('删除成功');
			proxy.$router.push('/addressList');
		}
	});
};
const onChangeDetail = (val) => {};
const emit = defineEmits(['isNav', 'isBar', 'loading']);
onMounted(() => {
	// 初始化活动状态
	emit('isBar');
	emit('isNav');
	emit('loading');
});
</script>

<style lang="scss" scoped>
.main {
	padding-top: 40px;
}
</style>
